﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows.Forms;
using InfoLayer;
using ControlLayer;

namespace GUI
{
    public partial class frmLapHoaDon : Form
    {
        private HoaDonControl hoaDonControl;
        private HoaDonInfo hoaDonInfo;
        private KhachHangControl khachHangControl;
        private PhongControl phongControl;
        private PhieuThuePhongControl phieuThuePhongControl;
        private PhieuThuePhongInfo phieuThuePhongInfo;
        private PhuThuControl phuThuControl;
        private PhuThuInfo phuThuInfo;
        private ThamSoControl thamSoControl;
        private LoaiKhachHangControl loaiKhachHangControl;

        public frmLapHoaDon()
        {
            InitializeComponent();
            hoaDonControl = new HoaDonControl();
            hoaDonInfo = new HoaDonInfo();
            khachHangControl = new KhachHangControl();
            phongControl = new PhongControl();
            phieuThuePhongControl = new PhieuThuePhongControl();
            phieuThuePhongInfo = new PhieuThuePhongInfo();
            phuThuControl = new PhuThuControl();
            phuThuInfo = new PhuThuInfo();
            thamSoControl = new ThamSoControl();
            loaiKhachHangControl = new LoaiKhachHangControl();
        }

        private void frmLapHoaDon_Load(object sender, EventArgs e)
        {
            DataTable dt;
            dt = khachHangControl.LayBangDanhSachKhachHang();
            comboBoxTenKhachHang.DataSource = dt;
            comboBoxTenKhachHang.DisplayMember = "TenKhachHang";
            comboBoxTenKhachHang.ValueMember = "MaKhachHang";
            comboBoxTenKhachHang.AutoCompleteMode = AutoCompleteMode.Suggest;
            comboBoxTenKhachHang.AutoCompleteSource = AutoCompleteSource.ListItems;
            //comboBoxTenKhachHang.SelectedIndex = 0;

            //Lay DS Phong da thue
            dt = phongControl.LayDSPhongDaThue();
            TenPhong.DataSource = dt;
            TenPhong.DisplayMember = "TenPhong";
            TenPhong.ValueMember = "MaPhong";
            
            
        }

        private void btnLapHoaDon_Click(object sender, EventArgs e)
        {
            if (comboBoxTenKhachHang.Text == "")
            {
                MessageBox.Show("Chọn tên khách hàng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
                

            //Kiem tra xem co phong nao da thue k?
            bool isCheck = false;

            DataTable dt;
            dt = hoaDonControl.LayBangDanhSachHoaDon();
            if (dt.Rows.Count > 0)
                hoaDonInfo.MaHoaDon = hoaDonControl.TaoMaTuDong(dt.Rows[dt.Rows.Count - 1][0].ToString(), 3);
            else
                hoaDonInfo.MaHoaDon = hoaDonControl.TaoMaTuDong("MHD000", 3);
            hoaDonInfo.NgayThanhToan = dateTimePickerNgayThanhToan.Value;
            hoaDonInfo.MaKhachHang = comboBoxTenKhachHang.SelectedValue.ToString();
            hoaDonInfo.TriGia = 0;

            //Cap nhat PHIEU THUE PHONG(so ngay thue, thanh tien, ma hoa don)
            int Rows = dataGridViewDSPhieuThue.Rows.Count;
            if (Rows != 0)
            {
                for (int i = 0; i < Rows - 1; i++)
                {
                    if (dataGridViewDSPhieuThue.Rows[i].Cells["TenPhong"].Value.ToString() != null)
                    {
                        isCheck = true;
                        float phuThu = 0;
                        float heSoLoaiKhach = 1;

                        //DataTable dt;
                        dt = phieuThuePhongControl.LayDSPhieu_ChuaTT(dataGridViewDSPhieuThue.Rows[i].Cells["TenPhong"].Value.ToString());

                        phieuThuePhongInfo.MaPhieuThue = dt.Rows[0][0].ToString();
                        phieuThuePhongInfo.MaPhong = dt.Rows[0][3].ToString();
                        phieuThuePhongInfo.DonGiaThue = Convert.ToInt32(dt.Rows[0][2].ToString());
                        phieuThuePhongInfo.NgayThuePhong = Convert.ToDateTime(dt.Rows[0][1].ToString());
                        TimeSpan songaythue = dateTimePickerNgayThanhToan.Value - phieuThuePhongInfo.NgayThuePhong;
                        
                        phieuThuePhongInfo.SoNgayThue = songaythue.Days;
                        phieuThuePhongInfo.ThanhTien = phieuThuePhongInfo.SoNgayThue * phieuThuePhongInfo.DonGiaThue;
                        phieuThuePhongInfo.MaHoaDon = hoaDonInfo.MaHoaDon;

                        

                        //Lay So Khach Theo Don Gia
                        dt = thamSoControl.LayBangThamSo();
                        int soKhachTheoDonGia = Convert.ToInt32(dt.Rows[0][1].ToString());
                        MessageBox.Show("so khach theo don gia " + soKhachTheoDonGia, "Thong bao", MessageBoxButtons.OK);

                        dt = phieuThuePhongControl.LaySoLuongKhach(phieuThuePhongInfo.MaPhieuThue);
                        int soLuongKhach = Convert.ToInt32(dt.Rows[0][0].ToString());
                        MessageBox.Show("so luong khach " + soLuongKhach, "Thong bao", MessageBoxButtons.OK);

                        if (soLuongKhach <= soKhachTheoDonGia)
                            phuThu = 0;
                        if (soLuongKhach > soKhachTheoDonGia)
                        {
                            int thuTuKhachTinhPhuThu;

                            dt = phuThuControl.LayBangDanhSachPhuThu();
                            int rows = dt.Rows.Count;
                            if (rows != 0)
                            {
                                MessageBox.Show("so rows bang phu thu " + rows, "Thong bao", MessageBoxButtons.OK);
                            }
                            for (int j = 0; j < rows ; j++)
                            {
                                thuTuKhachTinhPhuThu = Convert.ToInt32(dt.Rows[j][1].ToString());
                                MessageBox.Show("so thu tu khach tinh phu thu " + thuTuKhachTinhPhuThu, "Thong bao", MessageBoxButtons.OK);
                                if (soLuongKhach == thuTuKhachTinhPhuThu)
                                    phuThu = (float)Convert.ToDouble(dt.Rows[j][2].ToString());
                            }
                        }
 

                        //Kiem tra co khach nuoc ngoai co trong phieu thue phong k?
                        
                        dt = phieuThuePhongControl.LayLoaiKhach(phieuThuePhongInfo.MaPhieuThue);
                        int soluongkhach = dt.Rows.Count;
                        for (int j = 0; j < soluongkhach; j++)
                        {
                            DataTable _dt = loaiKhachHangControl.LayBangDanhSachLoaiKhachHang();
                            string maloaikhach1 = dt.Rows[j][0].ToString();
                            string maloaikhach2 = _dt.Rows[1][0].ToString(); //loai khach nuoc ngoai
                            if (maloaikhach1 == maloaikhach2)
                                heSoLoaiKhach = (float)Convert.ToDouble(_dt.Rows[1][2].ToString());
                            
                        }
                        MessageBox.Show("He so khach nuoc ngoai " + heSoLoaiKhach, "Thong bao", MessageBoxButtons.OK);

                        float thanhTien = (float)phieuThuePhongInfo.SoNgayThue * ((float)1 + phuThu) * (float)phieuThuePhongInfo.DonGiaThue * (float)heSoLoaiKhach;
                        phieuThuePhongInfo.ThanhTien = (int)thanhTien;

                        //Hien thi thong tin len DataGridView
                        dataGridViewDSPhieuThue.Rows[i].Cells["Stt"].Value = (i + 1).ToString();
                        dataGridViewDSPhieuThue.Rows[i].Cells["SoNgayThue"].Value = phieuThuePhongInfo.SoNgayThue;
                        dataGridViewDSPhieuThue.Rows[i].Cells["DonGia"].Value = phieuThuePhongInfo.DonGiaThue;
                        dataGridViewDSPhieuThue.Rows[i].Cells["ThanhTien"].Value = phieuThuePhongInfo.ThanhTien;

                        //Lay Tri gia HOA DON
                        hoaDonInfo.TriGia = hoaDonInfo.TriGia + phieuThuePhongInfo.ThanhTien;

                        //Update PHIEU THUE PHONG
                        phieuThuePhongControl.Update_DaTT(phieuThuePhongInfo);

                        //Update Tinh Trang PHONG = "Trong"
                        phongControl.Update_TinhTrangPhong(phieuThuePhongInfo.MaPhong, false);
                    }
                    
                }
                if (isCheck)
                {
                    textBoxTriGia.Text = hoaDonInfo.TriGia.ToString();

                    //Them HOA DON
                    hoaDonControl.Insert(ref hoaDonInfo);
                }
                else
                {
                    MessageBox.Show("Ban chua chon phong thanh toan", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }

            //khong cho nhap Lap hoa don nua
            btnLapHoaDon.Enabled = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void comboBoxTenKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt;
            dt = khachHangControl.LayKhachHang(comboBoxTenKhachHang.SelectedValue.ToString());
            if (dt.Rows.Count > 0)
            {
                textBoxCMND.Text = dt.Rows[0][3].ToString();
                textBoxDiaChi.Text = dt.Rows[0][4].ToString();
            }
            else
                return;
        }

        private void dataGridViewDSPhieuThue_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Hien thi STT Phong thanh toan
            if (dataGridViewDSPhieuThue.CurrentRow.Cells[0].Value != null)
                return;
            else
                dataGridViewDSPhieuThue.CurrentRow.Cells[0].Value = dataGridViewDSPhieuThue.Rows.Count.ToString();
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            comboBoxTenKhachHang.ResetText();
            dateTimePickerNgayThanhToan.ResetText();
            textBoxCMND.Clear();
            textBoxDiaChi.Clear();
            textBoxTriGia.Clear();
            dataGridViewDSPhieuThue.Rows.Clear();
            frmLapHoaDon_Load(sender, e);
            btnLapHoaDon.Enabled = true;
        }

         

        
    }
}
